import { observable, action, toJS } from 'mobx';
import { WeaTools } from 'ecCom';
import { message } from 'antd';
import * as urls from '../constants/ServerUrls';
import { getNewTreeData, getAddTreeData } from '../util/TreeUtil';

const { callApi } = WeaTools;

export default class FavouriteBrowserStore {
    /** 树节点的数据 */
    @observable treeData = [];

    /** 选中的树节点 */
    @observable selectedKey = '';
    @observable spinning = false;
    @observable tip = '';
    @observable testfatherdata = [];
    @observable testchildrendata =[];
    /**
     * 加载树节点的数据
     * @param parentId
     * @param resovle
     */
    @action loadTreedata = (parentId) => {
      const treeurl = urls.LOAD_TREE_URL;
      return new Promise((resovle, reject) => {
        callApi(treeurl, 'POST', { parentId }).then((resdata) => {
          const msg = resdata.msg;
          const success = resdata.success;
          if (success == '1') {
            const data = resdata.data;
            const newdata = [];
            /**
                     * 设置树节点的数据
                     */
            data.map((val, index) => {
              newdata.push({
                nodeid: val.favid,
                canClick: true, // 标识节点是否可以点击 （单选 多选模式下生效）
                icon: val.child == 1 ? 'icon-coms-LargeArea' : '', // 图标展示
                isParent: val.child == 1, // 父节点标识
                name: val.favname, // 名称
                dragDisabled: true, // 禁止节点拖拽
                ops: [],

              });
            });
            /**
                     * 关闭加载中的提示信息
                     */
            this.toggleSpinning(false);
            if (parentId == 0) {
              this.testfatherdata = newdata;
            } else {
              this.testchildrendata = newdata;
            }
            resovle(this.testchildrendata);
          }
        });
      });
    }
    /**
     * 关闭窗口时调用，清空数据
     */
    @action destroyBrowser = () => {
      this.treeData = [];
      this.selectedKey = '';
      this.spinning = false;
      this.tip = '';
    }


    /**
     * 节点选中
     * @param selectedKey
     */
    @action selectTreeNode = (selectedKey) => {
      this.selectedKey = selectedKey;
    }

    /**
     * 添加树节点
     * @param selectedKey
     */
    @action addTreeNode = (selectedKey) => {
      const treeData = toJS(this.treeData);
      const newData = getAddTreeData(treeData, selectedKey);
      this.treeData = newData;
    }

    /**
     * 保存树节点
     * @param params
     * @param resovle
     * @returns {function(*)}
     */
    @action saveTreeNode = (params, resovle) => {
      const favurl = urls.SAVE_FAVOURITE_DIR;
      const { parentid } = params;
      callApi(favurl, 'POST', { ...params }).then((resdata) => {
        const success = resdata.success;
        let msg = '';
        if (success == '0') {
          msg = resdata.msg;

          if (msg) {
            message.error(msg);
          }
        }

        // 刷新父节点
        this.loadTree(parentid, resovle);
      });
    }

    /**
     * 显示加载中的遮罩
     * @param spinning
     * @param tip
     */
    @action toggleSpinning = (spinning, tip = '') => {
      this.spinning = spinning;
      this.tip = tip;
    }


    /**
     * 设置树的节点数据
     * @param data
     * @param parentId
     */
    setTreeData(data, parentId) {
      const treeData = toJS(this.treeData);
      const newData = getNewTreeData(treeData, parentId, data);
      this.treeData = newData;
    }
}
